﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Pavilion
{
    public class NodeScriptCollection : IResourceCollection, IEnumerable<NodeScript>
    {
        public event Action<NodeScript> NodeScriptAdded, NodeScriptRemoved;

        private Dictionary<string, NodeScript> nodeScriptDictionary = new Dictionary<string, NodeScript>();

        public void Dispose()
        {
            while (nodeScriptDictionary.Count != 0)
                RemoveNodeScript(nodeScriptDictionary.Keys.First());

            nodeScriptDictionary = null;
        }

        static public NodeScriptCollection LoadFromDirectory(string resourcePath, string resourceName)
        {
            string nodeScriptPath = resourcePath + @"Node Scripts\";
            if (!Directory.Exists(nodeScriptPath))
                return null;

            NodeScriptCollection nodeScriptCollection = new NodeScriptCollection();
            IEnumerable<string> nodeScriptList = Directory.GetFiles(nodeScriptPath, "*.xml").Select((x) => Path.GetFileName(x).Replace(".xml", ""));

            foreach (string scriptName in nodeScriptList)
            {
                NodeScript nodeScript = new NodeScript(scriptName);
                nodeScript.LoadScript(nodeScriptPath, resourceName);
                nodeScriptCollection.AddNodeScript(nodeScript);
            }

            return nodeScriptCollection;
        }

        public void AddNodeScript(NodeScript nodeScript)
        {
            nodeScriptDictionary.Add(nodeScript.Name, nodeScript);
            nodeScript.NameChanged += NodeScript_NameChanged;
            OnNodeScriptAdded(nodeScript);
        }

        public void RemoveNodeScript(string name)
        {
            NodeScript nodeScript = nodeScriptDictionary[name];
            nodeScriptDictionary.Remove(name);
            nodeScript.NameChanged -= NodeScript_NameChanged;
            OnNodeScriptRemoved(nodeScript);
        }

        public NodeScript GetNodeScript(string name)
        {
            return nodeScriptDictionary[name];
        }

        private void OnNodeScriptAdded(NodeScript nodeScript)
        {
            if (NodeScriptAdded != null)
                NodeScriptAdded(nodeScript);
        }

        private void OnNodeScriptRemoved(NodeScript nodeScript)
        {
            if (NodeScriptRemoved != null)
                NodeScriptRemoved(nodeScript);
        }

        private void NodeScript_NameChanged(string oldName, string newName)
        {
            NodeScript nodeScriptToUpdate = nodeScriptDictionary[oldName];
            nodeScriptDictionary.Remove(oldName);
            nodeScriptDictionary.Add(newName, nodeScriptToUpdate);
        }

        public IEnumerator<NodeScript> GetEnumerator() { return nodeScriptDictionary.Values.GetEnumerator(); }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return nodeScriptDictionary.Values.GetEnumerator(); }
    }
}
